package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.jobLog.JobLogQueryDTO;
import com.sh.rbac.domain.entity.JobLog;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.JobLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务日志控制器
 *
 * @author wb
 * @since 2024-08-22
 */

@Tag(name = "任务日志管理", description = "任务日志相关操作")
@RestController
@RequestMapping("monitor/job-log")
public class JobLogController {

    @Autowired
    private JobLogService jobLogService;

    @Operation(summary = "分页查询任务日志列表", description = "根据任务日志条件查询参数进行查询")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = JobLog.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("hasAuthority('monitor:job-log:page')")
    public Result<Page<JobLog>> getPage(
            @Parameter(description = "当前页码", required = true, example = "1") @PathVariable Long current,
            @Parameter(description = "每页记录数", required = true, example = "10") @PathVariable Long limit,
            @RequestBody JobLogQueryDTO jobLogQueryDTO) {
        return Result.success(jobLogService.getPage(current, limit, jobLogQueryDTO));
    }


    @OperationLogger(module = "系统日志->任务日志管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除任务日志", description = "批量删除任务日志信息")
    @ApiResponse(responseCode = "200", description = "成功任务日志菜单")
    @DeleteMapping("batch-delete")
    @PreAuthorize("hasAuthority('monitor:job-log:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return jobLogService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统日志->任务日志管理", type = OperationTypeEnum.EXPORT)
    @Operation(summary = "导出任务日志", description = "导出任务日志信息")
    @ApiResponse(responseCode = "200", description = "成功导出任务日志信息")
    @GetMapping("export-data/{startCount}/{endCount}")
    @PreAuthorize("hasAuthority('monitor:job-log:export')")
    public void exportData(@PathVariable Integer startCount, @PathVariable Integer endCount, HttpServletResponse response) {
        jobLogService.exportData(startCount, endCount, response);
    }

}
